package com.book.dao.book.impl;

import com.book.dao.book.BookDao;
import com.book.pojo.Book;
import com.book.utils.DbUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao {

    @Override
    public int addBook(Book book) {
        String sql = "INSERT INTO books (title, author, available) VALUES (?, ?, ?)";
        try (Connection conn = DbUtils.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, book.getTitle());
            pstmt.setString(2, book.getAuthor());
            pstmt.setBoolean(3, book.isAvailable());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException("添加图书失败", e);
        }
    }

    @Override
    public int deleteBook(int bookId) {
        // 定义删除图书的 SQL 语句
        String sql = "DELETE FROM books WHERE id = ?";
        try (Connection conn = DbUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            // 设置查询参数，即根据 bookId 删除图书
            pstmt.setInt(1, bookId);

            // 执行删除操作，返回受影响的行数
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException("删除图书失败", e);
        }
    }

    @Override
    public int updateBook(Book book) {
        String sql = "UPDATE books SET title = ?, author = ?, available = ? WHERE id = ?";
        try (Connection conn = DbUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, book.getTitle());
            pstmt.setString(2, book.getAuthor());
            pstmt.setBoolean(3, book.isAvailable());
            pstmt.setInt(4, book.getId());

            return pstmt.executeUpdate(); // 返回影响的行数
        } catch (SQLException e) {
            throw new RuntimeException("更新书籍失败", e);
        }
    }

    @Override
    public List<Book> findAllBooks() {
        String sql = "SELECT * FROM books";
        List<Book> books = new ArrayList<>();
        try (Connection conn = DbUtils.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Book book = new Book();
                book.setId(rs.getInt("id"));
                book.setTitle(rs.getString("title"));
                book.setAuthor(rs.getString("author"));
                book.setAvailable(rs.getBoolean("available"));
                books.add(book);
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询所有图书失败", e);
        }
        return books;
    }

    @Override
    public Book findBookById(int id) {
        String sql = "SELECT * FROM books WHERE id = ?";
        try (Connection conn = DbUtils.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                Book book = new Book();
                book.setId(rs.getInt("id"));
                book.setTitle(rs.getString("title"));
                book.setAuthor(rs.getString("author"));
                book.setAvailable(rs.getBoolean("available"));
                return book;
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询图书失败", e);
        }
        return null;
    }


}
